#ifndef OBJECTS_H
#define OBJECTS_H

/* Copyright (C) 2011 Riccardo Marson

   This file is part of OpenPool, an open-source game of pool. 

   Permission is hereby granted, free of charge, to any person obtaining a
   copy of this software and associated documentation files (the "Software"),
   to deal in the Software without restriction, including without limitation
   the rights to use, copy, modify, merge, publish, distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included
   in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
   RICCARDO MARSON BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
   AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

  See Readme.rtf for further information.
  
*/

#include "interfaces.h"
#include "material.h"

#include <list>
#include <string>

//--------------------------
// classes defined here:

class CSphere;
class CPlane;
class CBox;
class CObject;
class CScriptObject;

//------------------------

//------------------------------------------------------------------------------
// Collision Shapes
//------------------------------------------------------------------------------

//CSphere
class CSphere : public ICollisionShape
{
	friend class CPlane;
	friend class CBox;

public:

    CSphere();
    CSphere(float radius);
    virtual ~CSphere();

    // interface
    eCollisionShape GetType();
    void SetParent(IObject* parent);
	IObject* GetParent();
	
    CollisionPair TestCollision(CollisionShapePtr other, bool& collided);

    //class-specific
    void SetRadius(float radius);
    float& GetRadius();

protected:

    eCollisionShape mType;
    float mRadius;

    IObject* mParent;
};

class CSocket : public CSphere
{
	friend class CPlane;
	friend class CBox;

public:

	CSocket() : CSphere() { mType = CS_SOCKET; }
	CSocket(float radius) : CSphere(radius) { mType = CS_SOCKET; }
	virtual ~CSocket() {}
};

//CPlane
class CPlane : public ICollisionShape
{
	friend class CSphere;
	friend class CBox;

public:

    CPlane();
    CPlane(CVector3& normal);
    virtual ~CPlane();

    // interface
    eCollisionShape GetType();
    void SetParent(IObject* parent);
	IObject* GetParent();
	
    CollisionPair TestCollision(CollisionShapePtr other, bool& collided);

    // class-specific
    void SetNormal(CVector3& normal);
    CVector3& GetNormal();

private:

    eCollisionShape mType;
    CVector3 mNormal;

    IObject* mParent;
};

//CBox
class CBox : public ICollisionShape
{
	friend class CSphere;
	friend class CPlane;

public:

    CBox();
    virtual ~CBox();

    // interface
    eCollisionShape GetType();
    void SetParent(IObject* parent);
	IObject* GetParent();
	
    CollisionPair TestCollision(CollisionShapePtr other, bool& collided);

    // class-specific
    void AddPlane(CVector3& offset, CVector3& normal);
    std::pair<CVector3, CollisionShapePtr> GetPlane(int index);

private:

    eCollisionShape mType;
    std::vector< std::pair<CVector3, CollisionShapePtr> > mPlanes;

    IObject* mParent;
};

//------------------------------------------------------------------------------
// Object Base Class
//------------------------------------------------------------------------------

class CObject : public IObject
{
	friend void SeparateObjects(CollisionShapePtr shape1, CollisionShapePtr shape2);

public:

    CObject();
    CObject( eObjectType type, eObjectID id, string name,
    		CVector3 initialPosition, CVector3 initialVelocity );
    virtual ~CObject();

    // identifiers getters & setters
    // getters return a constant reference
    virtual eObjectType& GetObjectType();
    virtual eObjectID& GetObjectID();
    virtual string& GetName();
    virtual void SetObjectType(eObjectType type);
    virtual void SetObjectID(eObjectID id);
    virtual void SetName(string name);

    // Physical properties getters & setters
    // getters return a constant reference
    virtual float& GetMass();
    virtual float& GetDragCoeff();
    virtual float& GetCrossSection();
    virtual void SetMass(float mass);
    virtual void SetDragCoeff(float cd);
    virtual void SetCrossSection(float Ae);

    //Position and orientation getters & setters
    // getters return a modifiable reference
    virtual CVector3& GetPosition();
    virtual CVector3& GetVelocity();
    virtual CVector3& GetHeading();
    virtual CVector3& GetRotationAxis();
    virtual float& GetRotationAngle();

    virtual void SetPosition(CVector3& pos);
    virtual void SetVelocity(CVector3& vel);
    virtual void SetHeading(CVector3& heading);
    virtual void SetRotationAxis(CVector3& axis);
    virtual void SetRotationAngle(float angle);

    // State flags getters and setters
    virtual bool IsActive();
    virtual void SetActive(bool active);
    virtual bool IsFalling();
    virtual void SetFalling(bool falling);

    virtual bool IsVisible();
    virtual void SetVisible(bool visible);

    // Collision shape and MeshInfo
    virtual void SetCollisionShape(CollisionShapePtr cs);
    virtual void SetMeshInfo(CMeshInfo& meshInfo);
    virtual CollisionShapePtr GetCollisionShape();
    virtual CMeshInfo& GetMeshInfo();

    // Physics system interface
    virtual void Move(IPhysicsSystem* physics);
    virtual CollisionPair TestCollision(CollisionShapePtr other, bool& collided);

protected:

    // Object identifiers
    string mName;
    eObjectType mType;
    eObjectID mID;

    //Physical properties
    float mMass;
    float mCD; //aerodynamic drag coefficient. 0.47 for a sphere.
    float mAe; //section exposed to the air

    //Object Collision Shape
    CollisionShapePtr mCollisionShape;

    //Object mesh info
    CMeshInfo mMeshInfo;

    //Position and orientation
    CVector3 mPosition; //position of the geometric center or reference point
    CVector3 mVelocity; //velocity of the geometric center or reference point
    CVector3 mHeading; //direction where the object is facing
    CVector3 mRotationAxis; //the aribitrary axis of rotation
    float mRotationAngle; //the current rotation angle around mRotationAxis

    //Object state flags
    bool mIsFalling; //entity is no longer resting/rolling on a surface
    bool mIsActive; //entity can be moved and/or acted upon
	bool mIsVisible; //entity is visible
	
	// reference to its script-side counterpart
	CScriptObject *mScriptLink;
	
};

// ----------------------------------------------------------------------------------------------------------------------------------------------------------
// AS Class
// This is a script-side wrapper of CObject that exposes a limited interface so that script-side functions can 
// check/set some properties. Avoids registering the code-side objects directly. 
//-----------------------------------------------------------------------------------------------------------------------------------------------------------

class CScriptObject
{
	friend class CObject; // really needed?

	// destructor is private to prevent any external enitty to call it by accident. The object destruction should only happen when refCount gets down to 0. 
	~CScriptObject() {}

public:

	CScriptObject(CObject *obj);
	
	// AS behaviour
	int AddRef() ;
	int Release() ;

	//CObject interface
	eObjectType GetType();
    eObjectID GetID();
    std::string GetName();
	
	CVector3 GetPosition(); 
	CVector3 GetVelocity();
	void SetPosition(CVector3& pos);
	void SetVelocity(CVector3& vel);
	
	bool IsActive();
    void SetActive(bool active);
    bool IsFalling();
    void SetFalling(bool falling);
	
private:
	
	// reference the "parent" code-side object
	CObject *mCodeObj;

	int mRefCount;
};

#endif // OBJECTS_H
